package com.bm.server.service.impl;

import com.bm.server.config.RedisKeyConfig;
import com.bm.server.type.SmsLogType;
import com.bm.server.util.*;
import com.bm.server.vo.R;
import com.bm.server.dao.SmsLogDao;
import com.bm.server.entity.SmsLog;
import com.bm.server.service.intf.SmsLogService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
@Service
public class SmsLogServiceImpl implements SmsLogService {
    @Resource
    private SmsLogDao logDao;
    @Override
    public R sendLoginSms(String phone) {
        if (StringUtil.isNotEmpty(phone)) {
            if (StringUtil.isPhone(phone)) {
                String num = NumUtil.createCode(4) + "";
                System.out.println("登录验证码:"+num);
                if (AliSmsUtil.sendSmsCode(phone, num, AliSmsUtil.SMS_TEM_LOGIN)){
                    //4.记录验证码 到Redis
                    JedisUtil.addString(RedisKeyConfig.SMS_CODE_LOGIN + phone, num, RedisKeyConfig.SMS_CODE_TIME);
                    //添加数据到表
                    logDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(), "完成登录验证码发送！", 1));
                    return Rutils.ok("嘿你丫瞅啥呢，去瞅你验证码是啥啊");
                } else {
                    //添加数据到表
                    logDao.insert(new SmsLog(phone, SmsLogType.登录验证码.getValue(), "登录验证码发送失败！", 2));
                }
            }
        }
        return Rutils.fail();
    }
}